<?php

class Application_Form_AddUser extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post')
        	->setAttrib('id', 'addUserForm');
        
        $this->clearDecorators();
        foreach($this->getElements() as $element) {
        	$element->removeDecorator('DtDdWrapper');
        	$element->removeDecorator('Label');
        }
        
        $decorators = array(
        		array('ViewHelper'),
        		array('Errors'),
        		array('Label', array('requiredSuffix' => ' *', 'class' => 'leftalign')),
        		array('HtmlTag', array('tag' => 'li')),
        );
        
        $username = $this->addElement('text', 'username', 
        		array('label' => 'Username'));
        $username = $this->getElement('username')
        		->addValidator('alnum')
        		->addValidator('Db_NoRecordExists', true, array('table' => 'users', 'field' => 'username'))
        		->setRequired(true)
        		->addFilter('StringTrim');
        $username->getValidator('alnum')
        		->setMessage('Your username should include letters and numbers only');
        $username->getValidator('Db_NoRecordExists')
        		->setMessage('Username already exists');
        $username->setDecorators($decorators);
        
        $password = $this->addElement('password', 'password',
        		array('label' => 'Password'));
        $password = $this->getElement('password')
        		->addValidator('stringLength', true, array(8))
        		->setRequired(true)
        		->addFilter('StringTrim');
        $password->getValidator('stringLength')
        		->setMessage('Your password is too short');
        $password->setDecorators($decorators);
        
        $confirmPassword = $this->addElement('password', 'confirmPassword', 
        		array('label' => 'Confirm Password'));
        $confirmPassword = $this->getElement('confirmPassword')
        		->addValidator('Identical', false, array('token' => 'password'))
        		->setRequired(true)
        		->addFilter('StringTrim');
        $confirmPassword->getValidator('Identical')
        		->setMessage('Password Confirmation does not match');
        $confirmPassword->setDecorators($decorators);
        
        $email = $this->addElement('text', 'email_address', 
        		array('label' => 'Email Address'));
        $email = $this->getElement('email_address')
        		->addValidator('EmailAddress')
        		->addValidator('Db_NoRecordExists', true, array('table' => 'users', 'field' => 'email_address'))
        		->setRequired(true)
        		->addFilter('StringTrim');
        $email->getValidator('EmailAddress')
        		->setMessage('Invalid email address');
        $email->getValidator('Db_NoRecordExists')
        		->setMessage('Email address already exists');
        $email->setDecorators($decorators);
        
        $confirmEmail = $this->addElement('text', 'confirmEmail',
        		array('label' => 'Confirm Email Address'));
        $confirmEmail = $this->getElement('confirmEmail')
        		->addValidator('Identical', false, array('token' => 'email_address'))
        		->setRequired(true)
        		->addFilter('StringTrim');
        $confirmEmail->getValidator('Identical')
        		->setMessage('Email Confirmation does not match');
        $confirmEmail->setDecorators($decorators);
        
        $firstName = $this->addElement('text', 'first_name', 
        		array('label' => 'First Name'));
        $firstName = $this->getElement('first_name')
        		->addValidator('Alpha', false, array('allowWhiteSpace' => true))
        		->addFilter('StringTrim');
        $firstName->getValidator('Alpha')
        		->setMessage('Invalid first name. Must contain alphabetic characters only');
        $firstName->setDecorators($decorators);
        
        $lastName = $this->addElement('text', 'last_name',
        		array('label' => 'Last Name'));
        $lastName = $this->getElement('last_name')
        ->addValidator('Alpha', false, array('allowWhiteSpace' => true))
        ->addFilter('StringTrim');
        $lastName->getValidator('Alpha')
        ->setMessage('Invalid last name. Must contain alphabetic characters only');
        $lastName->setDecorators($decorators);
        
        $role = $this->addElement('select', 'role',
        		array(
        			'label' => 'Role',
        			'value' => 'member',
        			'multiOptions' => array(
        					'member' => 'Member',
        					'admin' => 'Admin'
        			)
        		)
        );
        $role = $this->getElement('role');
        $role->setDecorators($decorators);
        
        $submit = $this->addElement('submit', 'add_user',
        		array('label' => 'Add User'));
        $submit = $this->getElement('add_user')
        		->setDecorators(array(
        				array('ViewHelper'),
        ));
        
        $cancel = $this->addElement('submit', 'cancel',
        		array('label' => 'Cancel'));
        $cancel = $this->getElement('cancel')
        		->setDecorators(array(
        				array('ViewHelper'),
        ));
        
        $this->addDisplayGroup(array('add_user', 'cancel'), 'submit_buttons', 
        		array('disableLoadDefaultDecorators' => true));
        $submitButtons = $this->getDisplayGroup('submit_buttons');
        $submitButtons->addDecorators(array(
        				array('FormElements', 'Form'),
        				array('HtmlTag', array('tag' => 'li', 'class' => 'submit')),
        ));
        
        $this->setDecorators(array(
        		'FormElements',
        		array('HtmlTag', array('tag' => 'ul')),
        		array(array('DivTag' => 'HtmlTag'),
        			array('tag' => 'div', 'id' => 'addUserDiv')),
        		'Form'
        ));
    }


}

